/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mappers;

import handler.DatabaseConnection;
import java.io.FileNotFoundException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Product;
import model.ProductionRequest;
import model.ProductionRequestLine;

/* @author Benjamin */
public class ProductionRequestLineMapper {

    private static ProductionRequestLineMapper instance;

    public static ProductionRequestLineMapper getInstance() {
        if (instance == null) {
            instance = new ProductionRequestLineMapper();
        }
        return instance;
    }

    public ArrayList<ProductionRequestLine> getPRequestLines(ProductionRequest productionRequest) throws SQLException, FileNotFoundException {
        ArrayList<ProductionRequestLine> productionRequestLines = new ArrayList<>();

        Statement stmt;
        
        stmt = DatabaseConnection.getInstance().getInstance().getConnection().createStatement();

        int prId = productionRequest.getId();

        String sql = "select * from productionRequestLine,product where ";
        sql += "productionRequestId = " + prId + " and productId = product.id;";
        System.out.println(sql);
        
        ResultSet rs = stmt.executeQuery(sql);

        while (rs.next()) {
            
            //Henter productet som objekt via ProductMapper.
            int productId = rs.getInt("productId");            
            Product product = ProductMapper.getInstance().getProduct(productId);
            
            //Øvrige attributter.
            String state = rs.getString("stateId");
            int amount = rs.getInt("amount");

            productionRequestLines.add(new ProductionRequestLine(productionRequest, product, state, amount));
        }

        return productionRequestLines;
    }

}
